﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AyaWo.Entity;
using Commen;
using DAL;

namespace WinService
{
    public class SoDo
    {
        //先获取 SO !=1  的数据 放入队列中 
        // 循环获取 key1,2,3,4,5 的数据值 并逐条查询   获取键值  并更新数据库中的键值    然后修改so的搜索标志
        public void ToDo()
        {
            try
            {
                SpiderContentService ctx = new SpiderContentService();
                List<SpiderContent> lsc = ctx.GetListSp();

                if (lsc == null || lsc.Count() == 0) { return; }

                foreach (var q in lsc)
                {
                    string ID = q.ID;
                    if (ID.IsNullOrWhiteSpace())
                        return;
                    string key1 = q.key1;
                    string key2 = q.key2;
                    string key3 = q.key3;
                    string key4 = q.key4;
                    string key5 = q.key5;

                    if (key1.NotNullOrWhiteSpace()) { var kv = GetKeyValue(key1); ctx.Upk1v1(ID, kv); }
                    if (key2.NotNullOrWhiteSpace()) { var kv = GetKeyValue(key2); ctx.Upk2v2(ID, kv); }
                    if (key3.NotNullOrWhiteSpace()) { var kv = GetKeyValue(key3); ctx.Upk3v3(ID, kv); }
                    if (key4.NotNullOrWhiteSpace()) { var kv = GetKeyValue(key4); ctx.Upk4v4(ID, kv); }
                    if (key5.NotNullOrWhiteSpace()) { var kv = GetKeyValue(key5); ctx.Upk5v5(ID, kv); }
                    if (key1.NotNullOrWhiteSpace() || key2.NotNullOrWhiteSpace() || key3.NotNullOrWhiteSpace() || key4.NotNullOrWhiteSpace() || key5.NotNullOrWhiteSpace())
                    {
                        ctx.UpHaso(ID,   key1,   key2,   key3,   key4,   key5);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error("SoDo : "+ex.Message); 
            }
        }
        /// <summary>
        /// 在线查询关键字的排名情况
        /// </summary>
        /// <param name="keyName"></param>
        /// <returns></returns>
        public KeyValueModel GetKeyValue(string keyName)
        {
            KeyValueModel kv = new KeyValueModel();
            try
            {
                var docText = HtmlHelper.PostHtmlContent("http://s.tool.chinaz.com/baidu/words.aspx", $"kw={WebHelper.UrlEncode(keyName)}&page=&by=0&pn=", "utf-8");
                if (docText.IsNullOrWhiteSpace()) {  kv.value = 0; return kv; }
                //加载源代码，获取文档对象
                var doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(docText);
                var key = doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[2]/form[1]/div[2]/ul[1]/li[2]/div[1]/p[1]/a[1]/span[1]").InnerText;
                var value = doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[2]/form[1]/div[2]/ul[1]/li[2]/div[2]/a").InnerText;
                kv.KeyName = key;
                kv.value = value.ToInt();
                return kv;
            }
            catch (Exception ex)
            {
                Log.Error("keyName : " + keyName);
                Log.Error("GetKeyValue : "+ex.Message);
                kv.value = 0;
                return kv;
            }
        }
    }


}
